//
//  ViewController.swift
//  FMDBDemo
//
//  Created by chenxinju on 16/5/18.
//  Copyright © 2016年 Innovation. All rights reserved.
//

import UIKit


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
      
    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
     // insertData() //直接插入方法
        
    // insertData1("mindn", scrore: 89) //拼接插入方法
    
    //    updateData(12, name: "liujiang") //修改
    
    //   updateData1(14, name: "xiugai")  //使用预编译方法
        
    //   deleteData(2)      //删除数据
        
//        selectData()
        
  //      transform()     //事物
        
        // queueSelect()   //串行队列
        
        
    }

    //***************************数据插入的操作
  private func insertData() {
    let sql = "INSERT INTO T_Person (name,scrore,sex) VALUES ('chenmoumou',100,'man');INSERT INTO T_Person (name,score,sex) VALUES ('abc',80,'gir');"
    
    //运行sql语句
    FMDBManager.shareInstance.db.executeUpdate(sql, withArgumentsInArray: nil)
    }
    
    //**********************拼接参数
    private func insertData1(name:String,scrore:Int) {
    
      let sql = "INSERT INTO T_Person (name,scrore) VALUES ('\(name)',\(scrore));"
        print(scrore)
      FMDBManager.shareInstance.db.executeStatements(sql)
    }
    
    //**********************修改
    private func updateData(personid: Int,name:String) {
       //person_id 修改行的唯一id号
     let sql = "UPDATE T_Person SET name = '\(name)' WHERE person_id = \(personid);"
        
     FMDBManager.shareInstance.db.executeStatements(sql)
    
    }
    
    //**********************预编译 传入数组
    private func updateData1(personid: Int,name:String) {
    
        let sql = "UPDATE T_Person SET name = ? WHERE person_id = ?;"
        
        FMDBManager.shareInstance.db.executeUpdate(sql, withArgumentsInArray: [name,personid])
    }
    
    //********************删除

    private func deleteData(personid:Int) {
    
        let sql = "DELETE FROM T_Person WHERE person_id = ?;"
    FMDBManager.shareInstance.db.executeUpdate(sql, withArgumentsInArray: [personid])
    
    }
    
    
    /*
     executeUpdate 和 executeStatements的区别
     1.一个是有参数 和没有参数
     2. executeStatements能够执行多条sql 语句 executeUpdate只能执行一条语句
    */
    
    //读取表格数据
    private func  selectData() {
      let sql = "SELECT * FROM T_Person"
        
  let res = FMDBManager.shareInstance.db.executeQuery(sql, withArgumentsInArray: nil)
        
        while res.next() {
          //读取数据
        let name = res.stringForColumn("name")
            print("名字是\(name)")
        
           let scrore = res.stringForColumnIndex(2)
            
            print("学号是\(scrore)")
        }
    }
    
    /**
      事物
    */
    
    private func  transform() {
    let start = CACurrentMediaTime()

    let sql = "UPDATE T_Person SET name = ? WHERE person_id = ?;"
        
        for i in 0...10000 {
          let name = "ios-\(i)"
            
            let age = random() % 50//随机数
            
        FMDBManagerQueue.shareInstance.queue.inTransaction({ (db,
            rollbaock) -> Void in
           db.executeUpdate(sql, withArgumentsInArray: [name,age])
            
            //如果失败了让事物回滚
//            if i == 500 {
//              rollbaock.memory = true
//                //回滚一定要使用return
//             return
//                
//            }
        })
            
        }
      print("\(CACurrentMediaTime() - start)")
    
    
    }
    
    
    private func queueSelect() {
    
        //queue 同步串行队列 会造成死锁
    FMDBManagerQueue.shareInstance.queue.inDatabase { (db) -> Void in
        
     let sql = "INSERT INTO T_Person (name,score) VALUES ('xueba',100);"
        
     db.executeStatements(sql)
       
        
     //在这个语句插入完成之后再插入一条语句
        //执行sql语句
//      FMDBManagerQueue.shareInstance.queue.inDatabase { (db) -> Void in
//         let sql = "INSERT INTO T_Person (name,score) VALUES ('xueba',100);"
//            db.executeStatements(sql)
//        }
     
    }
    
    }
    
}

